﻿<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>角色部门权限</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="/plugins/layui/css/layui.css" media="all">
</head>
<body>
  <div class="layui-form" lay-filter="dds-add-form" id="dds-add-form" style="padding: 8px 8px 0 0;">
    <div id="tree-menus"></div>
    <div class="layui-form-item layui-hide">
      <input type="button" lay-submit lay-filter="btn_submit" id="btn_submit" value="确定">
    </div>
  </div>
  <script src="/plugins/layui/layui.js"></script>
  <script>
      layui.config({
          base: '/plugins/layuiadmin/' //静态资源所在路径
      }).extend({
          index: 'lib/index' //主入口模块
      }).use(['index', 'tree', 'util', 'form'], function () {
          var $ = layui.$, form = layui.form, tree = layui.tree, util = layui.util, quary =getRequest();
          var roleid =quary["roleid"];
          var load_start = layer.load(2);

          $.ajax({
              type: "get",
              url: "role_menus",
              data: { roleid: roleid },
              dataType: "json",
              success: function (res) {
                  layer.close(load_start);
                  //部门显示
                  tree.render({
                      elem: '#tree-menus'
                      , data: res.data
                      , showCheckbox: true  //是否显示复选框
                      , id: 'tree-menus'
                  });
              }
          });

          //监听提交
          form.on('submit(btn_submit)', function (data) {
              var load_index = layer.load(2);
              var checkData = tree.getChecked('tree-menus'); //获取选中节点的数据

              var menus = GetCheckId(checkData);

              var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引  
              $(this).addClass("layui-btn-disabled").prop("disabled", true);
              //提交 Ajax 成功后，关闭当前弹层并重载表格
              $.ajax({
                  type: "post",
                  url: "role_menus",
                  data: { roleid: roleid, menus: menus.join(",") },
                  dataType: "json",
                  success: function (res) {
                      if (res.code == 0) {
                          layer.close(load_index);
                          layer.msg(res.msg, { time: 1 * 1000 }, function () {
                              parent.layer.close(index); //再执行关闭 
                          });
                      }
                      else {
                          layer.close(load_index);
                          layer.alert(res.msg)
                      }
                  }
              });
          });
      });

      //获取树型选择项值
      function GetCheckId(data) {
          var ids = [];//存储id的数组
          data.forEach(function (item) {
              ids.push(item.id);
              var children = GetCheckId(item.children);
              ids = ids.concat(children);
          });
          return ids;
      }

      //从当前页面的url地址中获取参数数据
      function getRequest() {
          var url = location.search; //获取url中"?"符后的字串
          var theRequest = new Object();
          if (url.indexOf("?") != -1) {
              var str = url.substr(1);
              strs = str.split("&");
              for (var i = 0; i < strs.length; i++) {
                  theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
              }
          }
          return theRequest;
      }
  </script>
</body>
</html>